const {ipcRenderer,remote,ipcMain,dialog} = require('electron');
// import $ from 'jquery';
// const $ = global.jQuery = require('jquery');
const path=require('path')
const iconv = require('iconv-lite');
const spawn = require('child_process').spawn;

// very important
// window.onload=function (){
//     document.getElementById('open').addEventListener('click',()=>{
//         // alert('you clicked it!')
//         console.log('you clicked it')
//         remote.getCurrentWindow().webContents.openDevTools()
//     })
// }
setTimeout(()=>{
        document.getElementById('open').addEventListener('click',()=>{
        // alert('you clicked it!')
        // console.log('you clicked it')
        // remote.getCurrentWindow().webContents.openDevTools()


        // let win=null;
        // win=new remote.BrowserWindow({
        //     webPreferences:{
        //         nodeIntegration:true,
        //     }
        // });
        // // win.loadFile('../main.html')
        // win.loadURL('https://gitee.com/masaikk/integra-tools')

            let messNode=document.getElementById('ma');
            messNode.innerText="After changing"

            setTimeout(()=>{
                renderHTMLText(messNode);
            })

        ipcRenderer.send('from-son',{name:'lbwww'})
            //send data to main process

    })
})

// const tf =require('@tensorflow/tfjs')

ipcRenderer.on('change-mess',(e)=>{
    let messNode=document.getElementById('ma');
    messNode.innerText="After changing"
    console.log($('#ma').html());

    // const model = tf.sequential();
    // model.add(tf.layer.dense({
    //     units: 1,
    //     inputShape:[1]
    // }))

    // model.compile

    // const trainX = [3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1];
    // const trainY = [1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3];
    // const m = tf.variable(tf.scalar(Math.random()));
    // const b = tf.variable(tf.scalar(Math.random()));
    //
    // function predict(x) {
    //     return tf.tidy(function() {
    //         return m.mul(x).add(b);
    //     });
    // }
    //
    // function loss(prediction, actualValues) {
    //     return prediction.sub(actualValues).square().mean();
    // }
    //
    // const learningRate = 0.01;
    // const optimizer = tf.train.sgd(learningRate);
    //
    //
    // function train() {
    //     optimizer.minimize(function() {
    //         const predsYs = predict(tf.tensor1d(trainX));
    //         stepLoss = loss(predsYs, tf.tensor1d(trainY))
    //         return stepLoss;
    //     });
    //     plot();
    // }

    // const child = require('child_process');
    //
    // let exe_sentence=path.join(path.dirname(__dirname),'/pages/js/al.sh')
    //
    // child.exec('bash '+exe_sentence, function(err, sto) {
    //     setTimeout(()=>{
    //         messNode.innerText = JSON.stringify(sto);
    //         console.log(sto);
    //     })//sto才是真正的输出，要不要打印到控制台，由你自己啊
    //
    //     // messNode.innerText = JSON.stringify(sto);
    //     // console.log(sto);
    //     // console.log(exe_sentence)
    //
    //     })


})


ipcRenderer.on('show-alert',(event,data) => {
    alert(JSON.stringify(data))
})


function renderHTMLText(node){
    const ls = spawn('ping', ['www.4399.com']);

    ls.stdout.on('data', (data) => {

        node.innerText=node.innerText+`\n${iconv.decode(data, 'GBK')}`
        console.log(`stdout: ${iconv.decode(data, 'GBK')}`);
    });

    ls.stderr.on('data', (data) => {
        node.innerText=node.innerText+`\n ${iconv.decode(data, 'GBK')}`
        console.log(`stderr: ${iconv.decode(data, 'GBK')}`);
    });

    ls.on('close', (code) => {
        node.innerText=node.innerText+`\nchild process exited with code ${code}`
        console.log(`child process exited with code ${code}`);
    });

    // node.innerText='In setTimeOut'
}
